package priv.lirenhe.javabase.charpter03_4_07;

/**
 * https://www.cnblogs.com/qiyu/p/3349667.html
 * TODO
 *
 * @author lirenhe
 * Apr 3, 2019 10:07:29 AM
 *
 */
public class FinalizeEscapeGC {

    public static FinalizeEscapeGC SAVE_HOOK = null;
    public String name;
    
    public FinalizeEscapeGC(String name) {
        this.name = name;
    }
    
    public void isAlive() {
        System.out.println("yes, i am still alive");
    }
    
    @Override
    protected void finalize() throws Throwable {
        super.finalize();
        System.out.println("finalize method executed!");
        System.out.println(this.name);
        FinalizeEscapeGC.SAVE_HOOK = this;
    }
    
    public static void main(String[] args) throws Throwable {
        SAVE_HOOK = new FinalizeEscapeGC("abc");
        SAVE_HOOK =null;
        System.gc();
        Thread.sleep(3000);
        if(SAVE_HOOK != null) {
            SAVE_HOOK.isAlive();
        } else {
            System.out.println("no, i am dead");
        }
        
        SAVE_HOOK =null;
        System.gc();
        Thread.sleep(500);
        if(SAVE_HOOK != null) {
            SAVE_HOOK.isAlive();
        } else {
            System.out.println("no, i am dead");
        }
    }
}